package com.woniuxy.reader.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.reader.entity.Author;
import com.woniuxy.reader.entity.Editor;
import com.woniuxy.reader.entity.Manager;
import com.woniuxy.reader.entity.User;
import com.woniuxy.reader.mapper.AuthorMapper;
import com.woniuxy.reader.mapper.EditorMapper;
import com.woniuxy.reader.mapper.ManagerMapper;
import com.woniuxy.reader.mapper.UserMapper;
import com.woniuxy.reader.service.ManagerService;
import com.woniuxy.reader.vo.AuthorVO;
import com.woniuxy.reader.vo.UserVO;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zh_o
 * @since 2020-10-09
 */
@Service
public class ManagerServiceImpl extends ServiceImpl<ManagerMapper, Manager> implements ManagerService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private AuthorMapper authorMapper;

    @Resource
    private ManagerMapper managerMapper;

    @Resource
    private EditorMapper editorMapper;

    /**
     * 管理员/编辑 唯一登录
     *
     * @param tel
     * @param password
     * @return
     */
    @Override
    public String login(String tel, String password) {
        if (managerMapper.selectOne(new QueryWrapper<Manager>().eq("manager_tel", tel)) == null) {
            if (editorMapper.selectOne(new QueryWrapper<Editor>().eq("editor_tel", tel)) == null) {
                return "不存在该用户";
            } else {
                Editor editor = editorMapper.selectOne(new QueryWrapper<Editor>().eq("editor_tel", tel));
                Md5Hash md5Hash = new Md5Hash(password, "woNiuReAdeR*79", 1024);
                System.out.println("加密后的密码----------------------->" + md5Hash.toString());
                if (editor.getPassword().equals(md5Hash.toString())) {
                    return "编辑登录成功";
                } else {
                    return "密码错误";
                }
            }
        } else {
            Manager manager = managerMapper.selectOne(new QueryWrapper<Manager>().eq("manager_tel", tel));
            if (manager.getPassword().equals(password)) {
                return "管理员登录成功";
            } else {
                return "密码错误";
            }
        }
    }

    /**
     * 获取用户详情列表
     * 用户、订阅联表
     *
     * @param vo
     * @return
     */
    @Override
    public IPage<User> listUserAndSubscribe(UserVO vo) {
        // 条件构造器
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        // 添加查询条件
        queryWrapper.like(StringUtils.isNotBlank(vo.getUsername()), User.USERNAME, vo.getUsername());
        // 分页
        Page<User> page = new Page<>(vo.getPageNum(), vo.getPageSize());
        return userMapper.listUserAndSubscribe(page, queryWrapper, vo);
    }

    /**
     * 作者列表
     * 作者、书籍联表
     *
     * @param vo
     * @return
     */
    @Override
    public Page<Author> listAuthorByBook(AuthorVO vo) {
        // 条件构造器
        QueryWrapper<Author> queryWrapper = new QueryWrapper<>();
        // 添加查询条件
        // 笔名或真实名
//        queryWrapper.like(StringUtils.isNotBlank(vo.getName()), Author.PAYMENT, vo.getName())
//                .or().like(StringUtils.isNotBlank(vo.getName()), Author.NAME, vo.getName());

        queryWrapper.like(StringUtils.isNotBlank(vo.getName()),"pen_name",vo.getName());
        queryWrapper.like(StringUtils.isNotBlank(vo.getTel()),"author_tel",vo.getTel());
        queryWrapper.eq(vo.getId()!=null,"author_id",vo.getId());
        //签约作者
        if(vo.getStatus()==1){
            queryWrapper.eq("level","签约写手");
        }//未签约作者
        else if(vo.getStatus()==2){
            queryWrapper.eq("level","未签约");
        }

        // 分页
        Page<Author> page = new Page<>(vo.getPageNum(), vo.getPageSize());
//        return authorMapper.listAuthorByBook(page, queryWrapper, vo);
        return authorMapper.selectPage(page,queryWrapper);
    }

    /**
     * 查询所有编辑
     * @return
     */
    @Override
    public List<Editor> selectAllEditor() {
        return editorMapper.selectList(null);
    }

    /**
     * 查询所有作者
     * @return
     */
    @Override
    public List<Author> selectAllAuthor() {
        return authorMapper.selectList(null);
    }

}
